import RoomType from "../entity/RoomType";
import DD from "../manager/dynamic_data_manager";
import JsonManager from "../manager/json_manager";
import MainManager from "../manager/main_manager";
import StorageManager from "../manager/storage_manager";

export class GameBalance {


    // 计算敌人血量
    static calculateEnemyHealth(baseHealth: number, level: number): number {
        let growthRate = 1.5; // 默认成长率
        if (level <= 5) {
            growthRate = 1.2; // 初期成长较低
        } else if (level <= 10) {
            growthRate = 1.5; // 中期成长
        } else {
            growthRate = 1.8; // 后期成长更高
        }

        const additionalScaling = 0.2 * Math.log2(level + 1); // 根据关卡进一步修正
        return baseHealth * Math.pow(growthRate + additionalScaling, level);
    }

    // 计算金币奖励
    static calculateGoldReward(level: number): number {
        const baseGold = 50; // 基础金币
        const levelGrowth = 10; // 每关增加的金币数量
        const randomFactor = Math.random() * 5; // 随机波动

        return Math.round(baseGold + level * levelGrowth + randomFactor);
    }

    // 计算技能材料奖励
    static calculateSkillMaterials(level: number): number {
        const baseMaterials = 1; // 最低奖励
        const growthRate = Math.floor(level / 5); // 每 5 关增加 1 个材料

        return Math.min(10, baseMaterials + growthRate); // 限制最高材料数为 10
    }

    static debugHps(level) {
        const rates = [1.3, 1.2, 1.3, 1.25, 1.4, 1.35, 1.5, 1.45, 1.00007, 1.15]; // 每个等级段的倍率
        cc.log('----------------------level is:' + level)
        cc.log('10hp', this.calculateHp(level, 10, rates))
        cc.log('30hp', this.calculateHp(level, 30, rates))

        cc.log('1000hp', this.calculateHp(level, 1000, rates))
    }

    static calculateHp(level, baseHp, rates) {
        if (level < 1 || level > 100) {
            throw new Error("等级必须在1到100之间");
        }
        if (rates.length !== 10) {
            throw new Error("倍率数组必须包含10个元素");
        }

        let hp = baseHp;
        let remainingLevel = level;

        for (let i = 0; i < rates.length; i++) {
            const maxLevelInSegment = Math.min(remainingLevel, 5); // 当前倍率段的最大等级
            // cc.log('rate:' + i)
            // cc.log(Math.pow(rates[i], maxLevelInSegment)); // 计算倍率段的HP)
            hp *= Math.pow(rates[i], maxLevelInSegment); // 计算倍率段的HP
            remainingLevel -= maxLevelInSegment; // 剩余等级减少

            if (remainingLevel <= 0) break; // 等级用完，退出循环
        }
        cc.log(hp)

        let rate = DD.instance.roomType == RoomType.Elite ? 1.5 : 1;
        return hp * rate;
    }


    // 计算技能材料奖励
    static calculateSkillAtk(oriId: string): number {
        let id = Number(oriId)
        let baseAtk = { 11: 6, 1: 6, 3: 6, 5: 6, 7: 6 }
        let skillExtra = JsonManager.instance.getDataByName('skill_extra')

        DD.instance.setAttackDAta()
        let skillExtraInfo = skillExtra[id]

        let level = StorageManager.instance.skillLevel[id]
        let extRate = DD.instance.caculateAtkEtraRate()
        let tmpAtk = baseAtk[id]

        if (id == 11) {
            cc.log('球伤害-------------------------')
            tmpAtk = tmpAtk * (1 + StorageManager.instance.ballStrenth)
            cc.log((tmpAtk + skillExtraInfo.atk * level), (1 + extRate), StorageManager.instance.characterLevel[1] * 5)
            cc.log('final:', Math.ceil((tmpAtk + skillExtraInfo.atk * level) * (1 + extRate)) + StorageManager.instance.characterLevel[1] * 5)
        }

        return Math.ceil((tmpAtk + skillExtraInfo.atk * level) * (1 + extRate)) + StorageManager.instance.characterLevel[1] * 5


    }



    // bonusData.forEach((data, index) => {
    //            let item = PoolManager.instance.createObjectByName('list_btn', this.content1)
    //             // 创建每一项
    //             // const item = cc.instantiate(this.itemPrefab);
    //             // item.setPosition(item.x,item.y*index)
    //             const label = item.getComponentInChildren(cc.Label);
    //             if (label) {
    //                 label.string = `${data.desc}`;
    //             }
    //             // 设置节点名称，便于定位
    //             item.name = `Item-${index}`;
    //             if(this.unlockedItems.includes(index)){
    //                 item.getComponent(cc.Button).interactable = true
    //             }else{
    //                 item.getComponent(cc.Button).interactable = false
    //             }

    //             // this.content1.addChild(item); // 添加到 Content
    //         });
}

// 示例使用
const enemyHealth = GameBalance.calculateEnemyHealth(5000, 8);
const goldReward = GameBalance.calculateGoldReward(8);
const skillMaterials = GameBalance.calculateSkillMaterials(8);
// cc.log("子弹攻击力",GameBalance.calculateSkillAtk("5"))
console.log(`敌人血量: ${enemyHealth}`);
console.log(`金币奖励: ${goldReward}`);
console.log(`技能材料: ${skillMaterials}`);
